<?php require_once("../../includes_view/header.php"); ?>
<?php
$codigo = $_GET["codigo"];
$codigoVeiculo = $_GET["codigoVeiculo"];
$acao = "FINALIZAR";
require_once("regras/controlador/oficinaController.php");
?>
<script type="text/javascript" src="<?php echo $url; ?>/assets/js/util.js"></script>
<script type="text/javascript">
    $(document).ready(function(){	
		
        $.post("ajax_gerarParcelasServicoOficina.php", { parcelas: $("#ddlparcelas option:selected").val(), valor: $("#txttotal").val() },function(data){				
            $("#tbparcelas tbody").html("");						
            for(i=0;i<data.length;i++){
                $("#tbparcelas tbody").append("<tr><td>"+data[i].parcela+"</td><td><input type='text' name='txtdataParcela[]' id='txtdataParcela_"+i+"' value='"+data[i].data+"' class='formularioMini' OnKeyUp=\"mascaraData(this,this.value);\" onkeypress=\"Formatadata(this,event)\"  maxlength='10'></td><td><input type='text' name='txtvalorParcela[]' id='txtvalorParcela_"+i+"' value='"+data[i].valor+"' class='formularioMini' onkeypress='return formatar_moeda(this,\".\",\",\",event);'></td></tr>");	
            }
        }, "json");
		 
        $("#ddlparcelas").change(function(){ 
            if($("#ddlparcelas option:selected").val() > 0){				
                $.post("ajax_gerarParcelasServicoOficina.php", { parcelas: $("#ddlparcelas option:selected").val(), valor: $("#txttotal").val() },function(data){				
                    $("#tbparcelas tbody").html("");						
                    for(i=0;i<data.length;i++){
                        $("#tbparcelas tbody").append("<tr><td>"+data[i].parcela+"</td><td><input type='text' name='txtdataParcela[]' id='txtdataParcela_"+i+"' value='"+data[i].data+"' class='formularioMini' OnKeyUp=\"mascaraData(this,this.value);\" onkeypress=\"Formatadata(this,event)\"  maxlength='10'></td><td><input type='text' name='txtvalorParcela[]' id='txtvalorParcela_"+i+"' value='"+data[i].valor+"' class='formularioMini' onkeypress='return formatar_moeda(this,\".\",\",\",event);'></td></tr>");	
                    }
                }, "json");									
            }else{
                $("#tbparcelas tbody").html("");	
            }
        });		
		
    });
	
    function validaDados(){
        if($("#ddlLoja option:selected").val() > 0){
            return true;
        }else{
            alert("Selecione uma loja");
            return false;
        }
    }
	
</script>
<div id="sistema">
    <div id="migalhas">
        <a class="mig1">Veículos</a>
        <a class="mig1">Controle de Oficina</a>
        <a class="mig2">Finalizar</a>
    </div>
    <div id="pesquisa"></div>    
    <br>
    <?php if (isset($class) and isset($mensagem)) { ?>  
        <div id="mensagens" class="<?= $class ?>"><?php echo $mensagem; ?></div>    
    <?php } ?>
    <?php if (count($despesas) > 0) { ?>
        <div id="grid">
            <table>
                <thead>
                    <tr>
                        <td>código</td>
                        <td>descrição</td>
                        <td>Valor</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $i = 0;
                    $total = 0;
                    foreach ($despesas as $despesa) {
                        echo "<tr>
				<td>" . $despesa["id"] . "</td>
				<td>" . utf8_encode($despesa["descricao"]) . "</td>
				<td>" . Util::dbMoeda($despesa["valor"]) . "</td>								
			      </tr>";
                        $i++;
                        $total+=$despesa["valor"];
                    }                      
                    ?>                     
                </tbody>
                <tfoot>
                    <tr>
                        <td>código</td>
                        <td>descrição</td>
                        <td>Valor</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    <?php } ?>
    <br>
    <div id="formulario">
        <form name="frmOficina" id="frmOficina" method="post" action="" onSubmit="return validaDados()">
            <div class="linha">
                <div class='esquerda'>
                    <label for="ddlLoja">Loja</label>
                    <select class="formularioDDL" name="ddlLoja" id="ddlLoja">
                        <option value="0">Selecione</option>
                        <?php
                        foreach ($lojas as $loja) {
                            echo "<option value='" . $loja["id"] . "'>" . utf8_encode($loja["nome"]) . "</option>";
                        }
                        ?>
                    </select>
                </div>
                <div class='direita'>&nbsp;</div>
            </div>

            <?php if (isset($total) && ($total > 0)) { ?> 
                <div class="linha">
                    <div class='esquerda'>
                        <label for="txttotalExibir">Total</label>
                        <input type="hidden" name="txttotal" id="txttotal" value="<?= $total ?>" class="formulario">
                        <input type="text" name="txttotalExibir" id="txttotalExibir" value="<?= Util::dbMoeda($total) ?>" class="formulario" disabled>
                    </div>
                    <div class='direita'>
                        <label for="ddlparcelas">Parcelas</label>
                        <select name="ddlparcelas" id="ddlparcelas" class="formularioDDL">
                            <option value="1">1</option>
                            <?php
                            for ($i = 2; $i <= 20; $i++) {
                                echo "<option value='" . $i . "'>" . $i . "</option>";
                            }
                            ?>
                        </select>   
                    </div>
                </div> 
                <br>
                <div id="grid">
                    <table id="tbparcelas">
                        <thead>
                            <tr>
                                <td>Parcela</td>
                                <td>Data Vencimento</td>
                                <td>Valor</td>
                            </tr>
                        </thead>
                        <tbody>                                 
                        </tbody>
                        <tfoot>
                            <tr>
                                <td>código</td>
                                <td>descrição</td>
                                <td>Valor</td>
                            </tr>
                        </tfoot>
                    </table>
                </div> 
                <?php
            } else {
                echo "<input type='hidden' name='txttotal' id='txttotal' value='0'>";
            }
            ?>
            <br>
            <div class="linha">
                <div class='esquerda'>            	
                    <input type="hidden" name="hdcodigo" id="hdcodigo" value="<?= $codigo ?>" class="formulario">
                    <input type="submit" name="btnFinalizar" id="btnFinalizar" value="finalizar" class="button">
                </div>            
            </div>        
        </form>
    </div>    
</div>
<?php require_once("../../includes_view/footer.php"); ?>